package cloud.seri.auth.config.dbmigrations

import cloud.seri.auth.domain.Authority
import cloud.seri.auth.domain.User
import cloud.seri.auth.security.AuthoritiesConstants

import com.github.mongobee.changeset.ChangeLog
import com.github.mongobee.changeset.ChangeSet
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query

import java.time.Instant

/**
 * Creates the initial database setup.
 */
@ChangeLog(order = "001")
class InitialSetupMigration
{

    @ChangeSet(order = "01", author = "initiator", id = "01-addAuthorities")
    fun addAuthorities(mongoTemplate: MongoTemplate)
    {
        val adminAuthority = Authority()
        adminAuthority.name = AuthoritiesConstants.ADMIN
        val userAuthority = Authority()
        userAuthority.name = AuthoritiesConstants.USER
        mongoTemplate.save(adminAuthority)
        mongoTemplate.save(userAuthority)
    }

    @ChangeSet(order = "02", author = "initiator", id = "02-addUsers")
    fun addUsers(mongoTemplate: MongoTemplate)
    {
        val adminAuthority = mongoTemplate.findOne(Query.query(Criteria.where("name").`is`(AuthoritiesConstants.ADMIN)), Authority::class.java)
//        adminAuthority.name = AuthoritiesConstants.ADMIN
        val userAuthority = mongoTemplate.findOne(Query.query(Criteria.where("name").`is`(AuthoritiesConstants.USER)), Authority::class.java)

        val systemUser = User()
        systemUser.login = "system"
        systemUser.password = "\$2a\$10\$mE.qmcV0mFU5NcKh73TZx.z4ueI/.bDWbj0T1BYyqP481kGGarKLG"
        systemUser.userName = "System"
        systemUser.realName = "系统管理员"
        systemUser.status = 0
        systemUser.sex = 1
        systemUser.email = "system@localhost"
        systemUser.activated = true
        systemUser.langKey = "en"
        systemUser.createdBy = systemUser.login
        systemUser.createdDate = Instant.now()
        mongoTemplate.save(systemUser)

        val anonymousUser = User()
        anonymousUser.login = "anonymoususer"
        anonymousUser.password = "\$2a\$10\$j8S5d7Sr7.8VTOYNviDPOeWX8KcYILUVJBsYV83Y5NtECayypx9lO"
        anonymousUser.userName = "Anonymous"
        anonymousUser.realName = "临时用户"
        anonymousUser.email = "anonymous@localhost"
        anonymousUser.activated = true
        anonymousUser.langKey = "en"
        anonymousUser.createdBy = systemUser.login
        anonymousUser.createdDate = Instant.now()
        mongoTemplate.save(anonymousUser)

        val adminUser = User()
        adminUser.login = "admin"
        adminUser.password = "\$2a\$10\$gSAhZrxMllrbgj/kkK9UceBPpChGWJA7SYIb1Mqo.n5aNLq1/oRrC"
        adminUser.userName = "admin"
        adminUser.realName = "超级管理员"
        adminUser.email = "admin@localhost"
        adminUser.activated = true
        adminUser.langKey = "en"
        adminUser.createdBy = systemUser.login
        adminUser.createdDate = Instant.now()
        mongoTemplate.save(adminUser)

        val userUser = User()
        userUser.login = "user"
        userUser.password = "\$2a\$10\$VEjxo0jq2YG9Rbk2HmX9S.k1uZBGYUHdUcid3g/vfiEl7lwWgOH/K"
        userUser.userName = "User"
        userUser.realName = "普通用户"
        userUser.email = "user@localhost"
        userUser.activated = true
        userUser.langKey = "en"
        userUser.createdBy = systemUser.login
        userUser.createdDate = Instant.now()
        mongoTemplate.save(userUser)
    }
}
